import {reqGetCartList,reqCheckCart,reqDeleteCart} from '@/api';


const state={
    cartInfoList:[]
};
const mutations={
    GETCARTLIST(state,cartInfoList){
        state.cartInfoList=cartInfoList;
    }
};
const actions={
    //获取购物车数据
    async getCartList({commit}){
        let result=await reqGetCartList();
        if(result.code==200){
            // console.log(result);
            commit('GETCARTLIST',result.data);
        }
    },
    //修改选中状态
    async updateCheckCart({commit},{skuID,isChecked}){
        let result=await reqCheckCart(skuID,isChecked);
        if(result.code==200){
            // console.log(result);
        }else{
            Promise.reject(new Error('fail'));
        }
    },
    //删除购物车商品
    async deleteCart({commit},{skuId}){
        let result=await reqDeleteCart(skuId);
        if(result.code==200){
            console.log(result);
        }else{
            Promise.reject(new Error('fail'));
        }
    }
};
const getters={
    cartList(state){
        return state.cartInfoList[0]||{};
    }
};

export default{
    state,
    mutations,
    actions,
    getters
}